#!/bin/bash

# 定义颜色常量
RED="\e[1;31m"
GREEN="\e[1;32m"
YELLOW="\e[1;33m"
BLUE="\e[1;34m"
MAGENTA="\e[1;35m"
CYAN="\e[1;36m"
RESET="\e[0m"

# 定义彩色打印函数
color_print() {
    local color=$1
    local message=$2
    echo -e "${!color}${message}${RESET}"
}

# # 引入颜色打印函数
# source ./color_print.sh
# 示例用法
color_print "GREEN" "mkdir ./bin"
color_print "RED" "This is a red message"
color_print "BLUE" "This is a blue message"

echo "if you don't know how to use:'./cmake.sh help'"
##cmake && make 出现在选项里会引起shell歧义
case $* in
"cmake make")
    # mkdir ./build
    if [ ! -d "./build/" ]
    then 
       echo -e "\e[1;32m mkdir ./build \e[0m"
        mkdir -p ./build
    fi

    # mkdir ./bin
    if [ ! -d "./bin" ]
    then 
        echo -e "\e[1;32m mkdir ./bin \e[0m"
        mkdir -p ./bin
    fi

    
    # cd ./build/ [and] rm ./* -r [and] cmake .. && make
    echo -e "\e[1;32m cd ./build/ \e[0m"
    cd ./build || exit
    echo -e "\e[1;31m rm ./* -r \e[0m"
    rm -rf ./*
    echo -e "\e[1;32m cmake .. && make \e[0m"
    cmake .. && make
    echo -e "\e[1;32m exit build \e[0m"
    cd .. || exit 
    echo -e "\e[1;36m finished \e[0m"
    ;;
"aarch64")
    # mkdir ./build
    if [ ! -d "./build/" ]
    then 
       echo -e "\e[1;32m mkdir ./build \e[0m"
        mkdir -p ./build
    fi

    # mkdir ./bin
    if [ ! -d "./bin" ]
    then 
        echo -e "\e[1;32m mkdir ./bin \e[0m"
        mkdir -p ./bin
    fi

    
    # cd ./build/ [and] rm ./* -r [and] cmake .. && make
    echo -e "\e[1;32m cd ./build/ \e[0m"
    cd ./build || exit
    echo -e "\e[1;31m rm ./* -r \e[0m"
    rm -rf ./*
    echo -e "\e[1;32m cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64.cmake && make \e[0m"
    cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64.cmake && make
    echo -e "\e[1;32m exit build \e[0m"
    cd .. || exit 
    echo -e "\e[1;36m finished \e[0m"
    ;;
"h37")
    # mkdir ./build
    if [ ! -d "./build/" ]
    then 
       echo -e "\e[1;32m mkdir ./build \e[0m"
        mkdir -p ./build
    fi

    # mkdir ./bin
    if [ ! -d "./bin" ]
    then 
        echo -e "\e[1;32m mkdir ./bin \e[0m"
        mkdir -p ./bin
    fi

    
    # cd ./build/ [and] rm ./* -r [and] cmake .. && make
    echo -e "\e[1;32m cd ./build/ \e[0m"
    cd ./build || exit
    echo -e "\e[1;31m rm ./* -r \e[0m"
    rm -rf ./*
    echo -e "\e[1;32m cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64.cmake && make \e[0m"
    cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/h37.cmake && make
    echo -e "\e[1;32m exit build \e[0m"
    cd .. || exit 
    echo -e "\e[1;36m finished \e[0m"
    ;;
"ubuntu-1804")
    # mkdir ./build
    if [ ! -d "./build/" ]
    then 
       echo -e "\e[1;32m mkdir ./build \e[0m"
        mkdir -p ./build
    fi

    # mkdir ./bin
    if [ ! -d "./bin" ]
    then 
        echo -e "\e[1;32m mkdir ./bin \e[0m"
        mkdir -p ./bin
    fi

    
    # cd ./build/ [and] rm ./* -r [and] cmake .. && make
    echo -e "\e[1;32m cd ./build/ \e[0m"
    cd ./build || exit
    echo -e "\e[1;31m rm ./* -r \e[0m"
    rm -rf ./*
    echo -e "\e[1;32m cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/ubuntu-1804.cmake && make \e[0m"
    cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/ubuntu-1804.cmake && make
    echo -e "\e[1;32m exit build \e[0m"
    cd .. || exit 
    echo -e "\e[1;36m finished \e[0m"
    ;;
"cmake")

    if [ ! -d "./build/" ]
    then 
        echo "mkdir build"
        mkdir ./build
    fi
    
    if [ ! -d "./bin" ]
    then 
        echo "mkdir bin"
        mkdir ../bin
    fi

    if [ ! -d "../bin/ortp" ]
    then 
        echo "mkdir ortp"
        mkdir ../bin/ortp
    fi

    cd ./linux-build || exit
    cmake ..
    ;;
"cmake clean")

    if [ ! -d "./build/" ]
    then 
        echo "no dir build, exit"
        exit
    fi
    cd ./build || exit
    echo "cd ./build & rm ./* -r"
    rm ./* -r
    ;;
"make")

    if [ ! -d "./linux-build/" ]
    then 
        echo "no dir linux-build, exit"
        exit
    fi

    cd ./build || exit

    if [[ -f "./makefile" ]] || [[ -f "./Makefile" ]] || [[ -f "./GUNMakefile" ]] 
    then 
        make
    else 
        echo "no makefile, exit,please './1.sh cmake'"
        exit
    fi
    ;;
"make clean")

    if [ ! -d "./build/" ]
    then 
        echo "no dir build, exit"
        exit
    fi

    cd ./linux-build || exit

    if [[ -f "./makefile" ]] || [[ -f "./Makefile" ]] || [[ -f "./GUNMakefile" ]] 
    then 
        make clean
    else 
        echo "no makefile, exit,please './1.sh cmake'"
        exit
    fi
    ;;
"help")
    echo "usage"
    echo "cmake make: './1.sh cmake make'"
    echo "cmake: './1.sh cmake'"
    echo "cmake clean: './1.sh cmake clean'"
    echo "make: './1.sh make'"
    echo "make clean: './1.sh make clean'"
    ;;
"run")
    ./bin/main
    ;;   
esac